package com.example.springcloud.controller;

import com.example.springcloud.service.OrderService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * The type Order controller.
 */
@RestController
@RequestMapping("/consumer")
@Slf4j
@DefaultProperties(defaultFallback = "globalTimeoutHandler", commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")})
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 测试连通
     *
     * @param id the id
     * @return the string
     */
    @GetMapping("/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {

        return orderService.paymentInfo_OK(id);
    }

    /**
     * 测试启用全局的降级处理方法
     *
     * @param id the id
     * @return the string
     */
    @HystrixCommand
    @GetMapping("/hystrix/timeout-global/{id}")
    public String paymentInfo_timeout01(@PathVariable("id") Integer id) {

        return orderService.paymentInfo_timeout(id);
    }

    /**
     * 测试单独的降级处理
     *
     * @param id the id
     * @return the string
     */
    @GetMapping("/hystrix/timeout-special/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeoutHandler", commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")})
    public String paymentInfo_timeout02(@PathVariable("id") Integer id) {

        return orderService.paymentInfo_timeout(id);
    }

    /**
     * 全局的降级的兜底方法（注意全局方法不能带参数，单独的方法需要带）
     *
     * @return the string
     */
    public String globalTimeoutHandler() {

        return "我是消费者80,现在连接不上服务端了/(ㄒoㄒ)/~~,由全局handler兜底";
    }

    /**
     * 单独的的降级的兜底方法
     *
     * @return the string
     */
    public String paymentInfo_TimeoutHandler(@PathVariable("id") Integer id) {

        return "我是消费者80,现在连接不上服务端了/(ㄒoㄒ)/~~";
    }

}
